

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:go_router/go_router.dart';
import 'package:local_auth/local_auth.dart';
import 'package:xim/config/route_config.dart';
import 'package:xim/http/apis.dart';
import 'package:xim/http/apis/user/user_api.dart';
import 'package:xim/http/apis/user/user_model.dart';
import 'package:xim/http/store.dart';

class FingerprintLoginPage extends StatefulWidget {

  final UserInfoModel user;

  const FingerprintLoginPage({super.key, required this.user});

  @override
  State<FingerprintLoginPage> createState() => _FingerprintLoginPageState();
}

class _FingerprintLoginPageState extends State<FingerprintLoginPage> {

  final auth = LocalAuthentication();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SizedBox(
        width: double.infinity,
        height: double.infinity,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            SizedBox(height: 0.1.sh,),
            CircleAvatar(
              backgroundImage: NetworkImage(widget.user.avatar),
            ),
            const SizedBox(height: 30,),
            Text("${widget.user.name} $hellHints",style: const TextStyle(fontSize: 23, color: Colors.black54)),
            SizedBox(height: 0.25.sh,),
            InkWell(
              onTap: () async {
                await enableFingerprint();
              },
              child: Icon(Icons.fingerprint_sharp, size: 38, color: Colors.red[300],),
            ),
            const SizedBox(height: 20,),
            InkWell(
                onTap: () async {
                  await enableFingerprint();
                },
                child: const Text("点击进行指纹解锁", style: TextStyle(fontSize: 13, color: Color(0x9E9E9E9E)),)
            ),
            const SizedBox(height: 50,),
            const Text("更多选择", style: TextStyle(fontSize: 14, color: Color(0xDD000000)))
          ],
        ),
      ),
    );
  }

  String get hellHints  {
    var nowHour = DateTime.now().hour;
    if(nowHour >= 6 && nowHour<=11) return "早上好";
    if(nowHour < 13) return "中午好";
    if(nowHour <= 23) return "晚上好";
    if(nowHour >= 0 && nowHour < 6 ) return "夜深了";
    return "";
  }

  enableFingerprint() async {
    var didAuthenticate = await auth.authenticate(
        localizedReason: '指纹登录验证'
    );
    if(didAuthenticate){
      // token 续签
      // var publicKey = await secureTake(StoreKey.publicKey);
      // var privateKey = await secureTake(StoreKey.privateKey);
      // if(publicKey!=null && privateKey!=null){
      //   var token = await UserApi.requestLogin(publicKey, privateKey);
      //   if(token!=null){
      //     storeString(StoreKey.token, token);
      //     Routers.toHome(type: RouteType.replaceAll);
      //   }
      // } else {
      //   toast("登录异常，请手动登录");
      //   if(context.mounted) context.go("/login/pwd");
      // }
    }
  }

}
